package com.lottery.im.module.chat.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.GroupNotice;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.Constants;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.chat.presenter.GroupNoticeEditPresenter;
import com.lottery.im.module.chat.view.GroupNoticeEditView;
import com.lottery.im.util.rxbus.RxBus;

/**
 * 公告新增编辑
 */
public class GroupNoticeEditFragment extends BaseSwipeFragment<GroupNoticeEditPresenter> implements
    GroupNoticeEditView {

  private final static String GROUPNOTICE_KEY = "GROUPNOTICE_KEY";

  public static GroupNoticeEditFragment create(GroupNotice groupNotice) {
    GroupNoticeEditFragment fragment = new GroupNoticeEditFragment();
    Bundle b = new Bundle();
    b.putSerializable(GROUPNOTICE_KEY, groupNotice);
    fragment.setArguments(b);
    return fragment;
  }

  private final static String ID_KEY = "ID_KEY";

  public static GroupNoticeEditFragment create(String id) {
    GroupNoticeEditFragment fragment = new GroupNoticeEditFragment();
    Bundle b = new Bundle();
    b.putString(ID_KEY, id);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.et_title) EditText mEtTitle;
  @BindView(R.id.et_content) EditText mEtContent;

  private GroupNotice mGroupNotice;
  private String mGroupId;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_group_notice_edit;
  }

  @Override protected void initPresenter() {
    presenter = new GroupNoticeEditPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {

    Bundle arguments = getArguments();
    if (null == arguments) {
      popBackStack();
      return;
    }
    RxBus.get().register(this);
    mGroupId = getArguments().getString(ID_KEY);
    mGroupNotice = (GroupNotice) getArguments().getSerializable(GROUPNOTICE_KEY);

    if (null == mGroupNotice) {
      mTvTitle.setText("新增群公告");
    } else {
      mTvTitle.setText("编辑群公告");
      String allContent = mGroupNotice.getContent();
      if (allContent.contains(Constants.NOTICE_SEPARATE)) {
        String title = allContent.substring(0, allContent.indexOf(Constants.NOTICE_SEPARATE));
        String content = allContent.substring(
            allContent.indexOf(Constants.NOTICE_SEPARATE) + Constants.NOTICE_SEPARATE.length(),
            allContent.length());
        mEtTitle.setText(title);
        mEtContent.setText(content);
      } else {
        mEtTitle.setText(allContent);
        mEtContent.setText(allContent);
      }
    }
  }

  @OnClick({ R.id.iv_back, R.id.tv_ok }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_ok:
        submit();
        break;
    }
  }

  private void submit() {
    String title = mEtTitle.getText().toString();
    String content = mEtContent.getText().toString();
    if (TextUtils.isEmpty(title)) {
      showToast("请输入标题");
      return;
    }
    if (TextUtils.isEmpty(content)) {
      showToast("请输入正文");
      return;
    }

    if (null == mGroupNotice) {
      presenter.groupNoticeAdd(mGroupId, title + Constants.NOTICE_SEPARATE + content, null);
    } else {
      presenter.groupNoticeAdd(mGroupNotice.getGroup_id(),
          title + Constants.NOTICE_SEPARATE + content, mGroupNotice.getId());
    }
  }

  @Override public void submitSuccess() {
    RxBus.get().send(RxBusCode.NOTICE_CODE,mEtTitle.getText().toString());
    popBackStack();
  }

  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }
}
